/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package openyugioh.service;

import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import openyugioh.dao.Dao;
import openyugioh.dao.IEntity;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Yeni
 */
@Transactional
public abstract class AbstractService<T extends IEntity> {
    
    @Resource
    private Dao<T, Serializable> statusDao;
    
    public AbstractService() {
    }
    
    protected abstract Class getEntityClass();
    
    @Transactional(readOnly=false, rollbackFor=Exception.class)
    public void save(T ct) {
        statusDao.save(ct);
    }
    @Transactional(readOnly=false, rollbackFor=Exception.class)
    public void delete(T ct){
        statusDao.delete(ct);
    }
    
    @Transactional(readOnly=true)
    public List<T> getStates() {
        DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
        criteria.addOrder(Order.asc("code"));
        return statusDao.get(criteria);
    }
    
    @Transactional(readOnly=true)
    public T getCardTypeByCode(String code) {
        DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
        criteria.add(Restrictions.eq("code", code));
        criteria.addOrder(Order.asc("code"));
        return statusDao.getUnique(criteria);
    }
}   